package com.imooc.ecommerce;

/**
 * ClassName: RedPacketSimulator
 * Package: com.imooc.ecommerce
 *
 * @Author 马学兴
 * @Create 2025-02-12 14:29
 * @Version 1.0
 * Description:
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RedPacketSimulator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 输入总金额和人数
        System.out.print("请输入红包总金额：");
        int totalAmount = scanner.nextInt();
        System.out.print("请输入抢红包总人数：");
        int totalPeople = scanner.nextInt();

        // 调用发红包方法
        List<Integer> redPackets = distributeRedPackets(totalAmount, totalPeople);

        // 输出结果
        System.out.println("每个人抢到的红包金额：");
        for (int i = 0; i < redPackets.size(); i++) {
            System.out.println("第 " + (i + 1) + " 个人抢到的红包金额为：" + redPackets.get(i) + " 元");
        }
    }

    public static List<Integer> distributeRedPackets(int totalAmount, int totalPeople) {
        List<Integer> redPackets = new ArrayList<>();
        Random random = new Random();

        // 剩余金额和剩余人数
        int remainingAmount = totalAmount;
        int remainingPeople = totalPeople;

        for (int i = 0; i < totalPeople - 1; i++) {
            // 每个人至少能抢到1元钱
            int max = remainingAmount - remainingPeople + 1;
            // 随机生成红包金额，范围是 [1, max]
            int amount = random.nextInt(max - 1) + 1;
            redPackets.add(amount);
            remainingAmount -= amount;
            remainingPeople--;
        }

        // 最后一个人拿剩下的所有金额
        redPackets.add(remainingAmount);

        return redPackets;
    }
}